package cn.xmstore.controller;

import cn.xmstore.entity.User;
import cn.xmstore.globals.BusinessCode;
import cn.xmstore.globals.CODE;
import cn.xmstore.globals.ResponseResult;
import cn.xmstore.service.UserService;
import cn.xmstore.util.JWTUtil;
import cn.xmstore.util.RedisUtil;
import cn.xmstore.util.RedisUtilImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author aeolia
 * @Date 2021/8/19 21:49
 */
@CrossOrigin(value = "http://34rr857292.zicp.vip", maxAge = 3600, originPatterns = "*", allowCredentials = "true")
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Resource(name = "redisTemplate")
    private RedisTemplate redisTemplate;

    @RequestMapping("/login")
    public String login() {
        return "login";
    }


    @GetMapping("/login")
    @ResponseBody
    public ResponseResult login(String account, String password, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<>();
        //账号密码非空判断
        if (account != null) {
            if (password != null) {
                //调用service进行账号登录验证
                User user = userService.login(account, password);
                //验证成功
                if (user != null) {
                    //生成token
                    String token = JWTUtil.generateToken(user.getId(), user.getName());
                    map.put("userId", user.getId());
                    map.put("userName", user.getName());
                    map.put("token", token);
                    //将token加入header中
                    response.setHeader("Token", token);
                    return new ResponseResult(CODE.XMS_REQUEST_SUCCESS, map);
                } else {
                    //验证失败
                    return new ResponseResult(CODE.XMS_USER_PASSWORD_ERROR, null);
                }
            } else {
                //返回用户密码为空
                return new ResponseResult(CODE.XMS_USER_PASSWORD_ISNULL, null);
            }
        } else {
            //返回用户账号为空
            return new ResponseResult(CODE.XMS_USER_ACCOUNT_ISNULL, null);
        }
    }

    @PutMapping("/register")
    @ResponseBody
    public ResponseResult register(String phoneNum, String checkCode) {

        //参数非空判断
        if (phoneNum == null || checkCode == null) {
            return new ResponseResult(CODE.XMS_PARAMETER_LACK, null);
        }
        //根据手机号从redis中获取对应的验证码
        String generateCode = (String) redisTemplate.opsForValue().get(BusinessCode.REGISTER.getCode() + phoneNum);
        //验证码在redis中会过期，过期后则不再能获取到，过期时间跟验证码有效时间相同

        //判断验证码是否存在或已过期
        if (generateCode == null || "".equals(generateCode)) {
            return new ResponseResult(CODE.XMS_CODE_TIMEOUT, null);
        }
        //判断验证码是否相同
        if (generateCode.equals(checkCode)) {
            //相同-验证成功
            //注册信息写入数据库
            userService.registerByPhoneNum(phoneNum);
            return new ResponseResult(CODE.XMS_REQUEST_SUCCESS, null);
        } else {
            //不同-验证失败
            return new ResponseResult(CODE.XMS_CODE_NOT_THE_SAME, null);
        }
    }

}
